require 'mechanize'

class Renshe
	def initialize( agent, mid, maxvalue, minvalue )
		@agent = agent
		@mid = mid
		@max = maxvalue
		@min = minvalue
		@peoples = Array.new
	end

	def parse_people()
		for page_num in 0..1
		page = @agent.get("http://friendsell.xiaonei.com/friendsell/hall.do?curpage=#{page_num}&i=0&s=2&sex=0")
		entrys = page.parser.search("//li[@class='frs-list-cell']")
		entrys.each do |entry|
			people = {}
			people[:id] = entry.search("/dl/dt/a")[0]["href"].match(/\?i=(\d*)/)[1]
			people[:name] = entry.search("/dl/dt/a")[0].inner_text
			people[:value] = entry.search("/dl/dd/span")[0].inner_text[/\d*$/].to_i
			@peoples << people if people[:value] <= @max and people[:value] >= @min
		end
		end
		@peoples
	end

	def list_people()
		@peoples.each do |p|
			puts "name:#{p[:name]}"
			puts "id:#{p[:id]}"
			puts "value:#{p[:value].to_s}"
			puts "--------------------"
		end
	end

	def buy_people()
		@peoples.each do |p|
			@agent.post("http://friendsell.xiaonei.com/friendsell/buy.do", { :id => p[:id], :mid => @mid })
		end
	end
end
